﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Company
{
    public partial class CompanyAddList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindAddCompanyList();
            }
        }

        #region 加载我加入的小组列表
        private void BindAddCompanyList()
        {
            Users uinfo = UserBase.GetCurrentUser();
            UserCompanyHelper uchelper = new UserCompanyHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
            c.Add(CriteriaType.Equals, "Status", 1);
            //c.Add(CriteriaType.NotEquals, "CompanyOwnerID", uinfo.ID);
            DataTable dt = uchelper.QueryUserCompanyBySearchToTable(c, null, 0, 0, null);

            dt.Columns.Add(new DataColumn("CompanyName"));
            dt.Columns.Add(new DataColumn("CompanyOwnerName"));
            dt.Columns.Add(new DataColumn("BlogPVNum"));
            dt.Columns.Add(new DataColumn("UserNum"));
            dt.Columns.Add(new DataColumn("CareNum"));
            dt.Columns.Add(new DataColumn("RealmName"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string companyid = dr["CompanyID"] + "";
                    string ownerid = dr["CompanyOwnerID"] + "";

                    //查找公司信息
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(companyid);
                    string companyname = cinfo.CompanyName;
                    dr["CompanyName"] = companyname;
                    dr["BlogPVNum"] = cinfo.BlogPVNum.ToString();
                    dr["UserNum"] = cinfo.UserNum.ToString();
                    dr["CareNum"] = cinfo.CareNum.ToString();
                    //公司所有人信息
                    dr["CompanyOwnerName"] = cinfo.OwnerNickName;
                    dr["RealmName"] = cinfo.RealmName.ToString();
                }

                lblAddCompany.Visible = false;
                rptAddCompanyList.DataSource = dt;
                rptAddCompanyList.DataBind();
            }
            else
            {
                lblAddCompany.Visible = true;
                rptAddCompanyList.Visible = false;
                lblAddCompany.Text = "<font color=\"red\">暂时没有加入的小组</font>";
            }
        }
        #endregion

        #region 加入的小组列表事件
        protected void rptAddCompanyList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string id = hidID.Value;
            if (e.CommandName == "cancel")//退出该公司
            {
                UserCompany ucinfo = new UserCompany();
                UserCompanyHelper uchelper = new UserCompanyHelper();
                ucinfo = uchelper.GetUserCompany(id);

                //给公司所有人发送消息
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                minfo.FromUsersID = ucinfo.UsersID;
                minfo.FromNickName = ucinfo.NickName;
                minfo.ToUsersID = ucinfo.CompanyOwnerID;
                minfo.MessageContent = ucinfo.NickName + "退出了小组";
                minfo.Status = 0;
                mhelper.AddMessage(minfo);

                uchelper.DeleteUserCompany(ucinfo);//删除 

                //该公司的用户数减1
                string companyid = ucinfo.CompanyID;
                Companys cinfo = new Companys();
                CompanysHelper chelper = new CompanysHelper();
                cinfo = chelper.GetCompanys(companyid);
                int usernum = cinfo.UserNum;
                usernum = usernum - 1;
                cinfo.UserNum = usernum;
                chelper.EditCompanys(cinfo);

                Tools.AlertAndRedirect("已经退出该小组", "CompanyAddList.aspx");
            }
        }
        #endregion
    }
}